import pymysql.cursors
import configparser

from pymysql import OperationalError

__all__ = ['dao']

config = configparser.ConfigParser()
config.read('db.ini')
mysql_db=config['mysql']
host=mysql_db['host']
port=int(mysql_db['port'])
user=mysql_db['user']
password=mysql_db['password']
db=mysql_db['db']

try:
    connection = pymysql.connect(host=host,
                                 port=port,
                                 user=user,
                                 password=password,
                                 db=db,
                                 charset='utf8mb4',
                                 cursorclass=pymysql.cursors.DictCursor)
except OperationalError as e:
    print(e)
else:
    print('连接成功')

# try:
#     with connection.cursor() as cursor:
#         # Create a new record
        # insert_topic_sql = "insert into `py_xzb_topic` (`id`, `topic_title`, `topic_date`, `article`, `light_gallery`,`user_id`) " \
#         insert_topic_sql = "insert into `py_xzb_topic` (`id`, `topic_title`, `article`, `light_gallery`,`user_id`) values (%s, %s, %s, %s, %s)"
#         cursor.execute(insert_topic_sql,('12', '测试title','似懂非懂','df/img.', 'dsfxcvsdf'))
#         print('保存一条数据到数据库')
#         # connection is not autocommit by default , So you must commit to save your changes
#     connection.commit()
#
#     # with connection.cursor() as cursor:
#     #     # Read s single record
#     #     sql = "select `id`, `password` from `py_users` where  `email`=%s"
#     #     cursor.execute(sql, ('test@qq.com',))
#     #     # result = cursor.fetchone()
#     #     # results = cursor.fetchall()
#     #     resultPage = cursor.fetchmany(2)
#     #     print(resultPage)
#
# finally:
#     connection.close()